package com.happle.gcmclient.service;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.happle.gcmclient.backendmanager.BackendManager;
import com.happle.gcmclient.config.CommonUtilities;
import com.happle.gcmclient.config.Constants;
import com.happle.gcmclient.config.GlobalValue;
import com.happle.gcmclient.managers.LngManager;
import com.happle.gcmclient.managers.MessagesManager;
import com.happle.gcmclient.obj.ConversationObj;
import com.happle.gcmclient.obj.ConversationsNotSyncObj;
import com.happle.gcmclient.obj.LngObj;
import com.happle.gcmclient.obj.MessageObj;
import com.happle.gcmclient.utility.NetworkUtility;

/**
 * Sync Data Service : Auto get message not synced to server and send to server
 * 
 * @author Dang
 * 
 * @created 08/03/2014
 * 
 */
public class SyncDataService extends Service {

	private static SyncDataService instance;

	private static final String TAG = "SyncService";
	private Context self;
	//private static int SYNC_INTERVAL = 1 * 60 * 1000; // 1 min for testing

	private Timer timer;
	private MessagesManager msgManager;
	private BackendManager backendManager;
	private LngManager languageManager;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	public static boolean isRunning() {
		return instance != null;
	}

	@Override
	public void onCreate() {
		self = this;
		instance = this;
		// Toast.makeText(this, "Sync Service Created",
		// Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");
		timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				syncData();
			}
		}, Constants.SYNC_INTERVAL_DELAY, Constants.SYNC_INTERVAL);
	}

	private void syncData() {
		Log.d(TAG, "Start check data to sync");
		msgManager = new MessagesManager(self);
		backendManager = new BackendManager();
		languageManager = new LngManager(self);

		ArrayList<LngObj> listlanguage = (new LngManager(self))
				.getAllLanguagesToSync();
		ArrayList<MessageObj> listNewObj = (new MessagesManager(self))
				.getAllMessageNotSend();
		ArrayList<ConversationsNotSyncObj> listUpdateObj = (new MessagesManager(self))
				.getNotSynchronizedConversations();
		// Check at least one list has data to sync
		if (listlanguage.size() > 0 || listNewObj.size() > 0
				|| listUpdateObj.size() > 0) {
			// Check if network is available
			if (NetworkUtility.getInstance(self).isNetworkAvailable()) {
				int response = backendManager.syncData(self, listlanguage,
						listNewObj, listUpdateObj, GlobalValue.registerId);
				if (response == 1) {
					Log.d(TAG, "Sync data successfully");
					for (MessageObj messageObj : listNewObj) {
						messageObj.set_is_sent(true);
						msgManager.updateMessage(messageObj);
					}
					
					for (LngObj item : listlanguage) {
						item.set_is_sent(true);
						languageManager.updateLngStatus(item);
					}
                    msgManager.deleteSynchronizedConversations(listUpdateObj);
				} else
					Log.d(TAG, "Sync data error");
			}
		} else
			Log.d(TAG, "No data to sync");

	}

	@Override
	public void onDestroy() {
//		Toast.makeText(this, "Sync Service Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
		instance = null;
		timer.cancel();
	}

	@Override
	public void onStart(Intent intent, int startid) {
//		Toast.makeText(this, "Sync Service Started", Toast.LENGTH_LONG).show();

	}

}
